package com.prs.crm.service.warning.impl;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

import com.prs.crm.context.AppContext;
import com.prs.crm.dao.warning.TaskRemindDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.warning.TaskRemind;
import com.prs.crm.domain.warning.WarnContent;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.impl.BaseServiceImpl;
import com.prs.crm.service.warning.TaskRemindService;
import com.prs.crm.service.warning.WarnContentService;
import com.prs.crm.tools.ControlJMail;
import com.prs.crm.tools.Datetools;
import com.prs.crm.tools.NoteTools;

/**
 * @author JohnLan
 * @time：2012-11-22 
 * @version 1.0
 * 类说明
 */

public class TaskRemindServiceImpl extends
		BaseServiceImpl<TaskRemind, TaskRemindDao> implements TaskRemindService {
	private WarnContentService warnContentService;
	private PersonService personService;
	private TaskRemindService taskRemindService;
	private Datetools dt=new Datetools();
	private NoteTools nt=new NoteTools();
	private ControlJMail cj=new ControlJMail();
	@Override
	public List<TaskRemind> getTaskRemind() {
		List<TaskRemind> remindList=this.getDao().getTaskRemind();
		
		for(TaskRemind tr:remindList)
		{
		int sy=0;
		if("开始之前".equals(tr.getResty()))
		{
		int ints=dt.getcurrentB(tr.getRestartTime());
		if(ints==1)//满足需求
		{
	    if("系统".equals(tr.getSendtype()))
	    {
	    WarnContent wc=new WarnContent();	
	    	wc.setWarnstyl(tr.getRestyle());
	    	wc.setWarnCont(tr.getReContent());
	    	wc.setReceivID(tr.getReId());
	    	wc.setReadState("1");
	     this.getWarnContentService().save(wc);	
	     sy++;
	    }else if("短信".equals(tr.getSendtype()))
	    {
	    //电话号码
	    Person per=	this.getPersonService().get(Integer.parseInt(tr.getReId()));
	    	try {
				nt.SendNote(per.getUserTel(),tr.getReContent(),dt.getstrTime(new Date()));
				sy++;
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}	
	    		    	
	    }else if("邮件".equals(tr.getSendtype()))
	    {
	    	/**
	    	 * 发送邮件
	    	 * 
	    	 * @param from
	    	 *            邮件发送人的邮件地址
	    	 * @param to
	    	 *            邮件接收人的邮件地址
	    	 * @param username
	    	 *            发件人的邮件帐户
	    	 * @param password
	    	 *            发件人的邮件密码
	    	 * @param object
	    	 *            邮件标题
	    	 * @param content
	    	 *            邮件内容
	    	 */
	    	Person per=	this.getPersonService().get(Integer.parseInt(tr.getReId()));
	    	Person curper=AppContext.getAccountContext().getUser();
	    	cj.sendMail(curper.getEmail(),per.getEmail(),"admin","admin","任务邮件",tr.getReContent());	
	    	sy++;
          }
		}
			
		}else if("结束之前".equals(tr.getResty()))
		{
			
			int ints=dt.getcurrentA(tr.getRestartTime(),tr.getReendTime());
			if(ints==1)//满足需求
			{
				 if("系统".equals(tr.getSendtype()))
				    {
				    WarnContent wc=new WarnContent();	
				    	wc.setWarnstyl(tr.getRestyle());
				    	wc.setWarnCont(tr.getReContent());
				    	wc.setReceivID(tr.getReId());
				    	wc.setReadState("1");
				     this.getWarnContentService().save(wc);	 
				 	sy++;
				    }else if("短信".equals(tr.getSendtype()))
				    {
				    //电话号码
				    Person per=	this.getPersonService().get(Integer.parseInt(tr.getReId()));
				    	try {
							nt.SendNote(per.getUserTel(),tr.getReContent(),dt.getstrTime(new Date()));
							sy++;
						} catch (MalformedURLException e) {
							e.printStackTrace();
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}	
				    		    	
				    }else if("邮件".equals(tr.getSendtype()))
				    {
				    	/**
				    	 * 发送邮件
				    	 * 
				    	 * @param from
				    	 *            邮件发送人的邮件地址
				    	 * @param to
				    	 *            邮件接收人的邮件地址
				    	 * @param username
				    	 *            发件人的邮件帐户
				    	 * @param password
				    	 *            发件人的邮件密码
				    	 * @param object
				    	 *            邮件标题
				    	 * @param content
				    	 *            邮件内容
				    	 */
				    	Person per=	this.getPersonService().get(Integer.parseInt(tr.getReId()));
				    	Person curper=AppContext.getAccountContext().getUser();
				    	cj.sendMail(curper.getEmail(),per.getEmail(),"admin","admin","任务邮件",tr.getReContent());
				    	sy++;
			          }
			}
			
			
		}else if("超期之后".equals(tr.getResty()))
		{
			int ints=dt.getcurrentC(tr.getReendTime());
			if(ints==1)//满足需求
			{
				 if("系统".equals(tr.getSendtype()))
				    {
				    WarnContent wc=new WarnContent();	
				    	wc.setWarnstyl(tr.getRestyle());
				    	wc.setWarnCont(tr.getReContent());
				    	wc.setReceivID(tr.getReId());
				    	wc.setReadState("1");
				     this.getWarnContentService().save(wc);	 
				 	sy++;
				    }else if("短信".equals(tr.getSendtype()))
				    {
				    //电话号码
				    Person per=	this.getPersonService().get(Integer.parseInt(tr.getReId()));
				    	try {
							nt.SendNote(per.getUserTel(),tr.getReContent(),dt.getstrTime(new Date()));
							sy++;
						} catch (MalformedURLException e) {
							e.printStackTrace();
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}	
				    		    	
				    }else if("邮件".equals(tr.getSendtype()))
				    {
				    	/**
				    	 * 发送邮件
				    	 * 
				    	 * @param from
				    	 *            邮件发送人的邮件地址
				    	 * @param to
				    	 *            邮件接收人的邮件地址
				    	 * @param username
				    	 *            发件人的邮件帐户
				    	 * @param password
				    	 *            发件人的邮件密码
				    	 * @param object
				    	 *            邮件标题
				    	 * @param content
				    	 *            邮件内容
				    	 */
				    	Person per=	this.getPersonService().get(Integer.parseInt(tr.getReId()));
				    	Person curper=AppContext.getAccountContext().getUser();
				    	cj.sendMail(curper.getEmail(),per.getEmail(),"admin","admin","任务邮件",tr.getReContent());
				    	sy++;
			          }
			}
			if(	sy>0)
			{
				this.getTaskRemindService().submit(tr);	
			}
			
		}
			
			
			
		
		
		}
			
			
		
		
		return this.getDao().getTaskRemind();
	}
	public WarnContentService getWarnContentService() {
		return warnContentService;
	}
	public void setWarnContentService(WarnContentService warnContentService) {
		this.warnContentService = warnContentService;
	}
	public Datetools getDt() {
		return dt;
	}
	public void setDt(Datetools dt) {
		this.dt = dt;
	}
	public PersonService getPersonService() {
		return personService;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	public NoteTools getNt() {
		return nt;
	}
	public void setNt(NoteTools nt) {
		this.nt = nt;
	}
	public TaskRemindService getTaskRemindService() {
		return taskRemindService;
	}
	public void setTaskRemindService(TaskRemindService taskRemindService) {
		this.taskRemindService = taskRemindService;
	}
	public ControlJMail getCj() {
		return cj;
	}
	public void setCj(ControlJMail cj) {
		this.cj = cj;
	}

}
